/**
 * @Title: index.js
 * @Description: index
 * @author: Fanchao
 * @date: 2020/12/13 8:13 下午
 * @version V1.0
 */
import React, { useEffect, useState } from "react";
import styles from "./index.less";
import { Button, DatePicker, Form, Input, message, Modal, Popconfirm, Select, Table } from "antd";
import {
    createRunTrainTaskInfo,
    deleteMajorDirection,
    getMajorDirectionList,
    getRunTrainTaskInfoList,
    getTrainingTypeList
} from "api/index";
import SectionTitle from "components/SectionTitle";
import AddSomeButton from "components/AddSomeButton";
import moment from "moment";

const { Option } = Select;

const { RangePicker } = DatePicker;
const layout = {
    labelCol: { span: 5 },
    wrapperCol: { span: 19 }
};
const TrainTask = () => {

    const [loading, setLoading] = useState(false);
    const [dataSource, setDataSource] = useState([]);
    const [visible, setVisible] = useState(false);
    const [trainingTypeList, setTrainingTypeList] = useState([]);
    const [trainingTypeId, setTrainingTypeId] = useState("");
    const [majorDirectionList, setMajorDirectionList] = useState([]);
    const [form] = Form.useForm();
    const fetch = async (filter) => {
        setLoading(true);
        const result = await getRunTrainTaskInfoList({ trainingTypeId: filter });
        if (result.status === "success") {
            const dataSource = result.result;
            setDataSource(dataSource);
        } else {
            message.error(result.message);
        }
        setLoading(false);
    };
    const fetchTrainingTypeList = async () => {
        const result = await getTrainingTypeList();
        if (result.status === "success") {
            const trainingTypeList = result.result;
            setTrainingTypeList(trainingTypeList);
        } else {
            message.error(result.message);
        }
    };
    const fetchMajorDirectionList = async (filter) => {
        const result = await getMajorDirectionList({ trainingTypeId: filter });
        if (result.status === "success") {
            const majorDirectionList = result.result;
            setMajorDirectionList(majorDirectionList);
        } else {
            message.error(result.message);
        }
    };
    useEffect(() => {
        fetchTrainingTypeList();
        fetch("");
    }, []);
    const changeSelect = (e) => {
        setTrainingTypeId(e);
    };
    const addItem = () => {
        setVisible(true);
    };
    const handleDelete = async ({ majorDirectionId }) => {
        const result = await deleteMajorDirection({ majorDirectionId });
        if (result.status === "success") {
            message.success("删除成功");
            fetch("");
            setTrainingTypeId("");
        } else {
            message.error(result.message);
        }
    };
    const title = "新增任务运行";
    const columns = [
        {
            title: "培训类型",
            dataIndex: "trainTypeName",
            align: "left"
        },{
            title: "专业方向",
            dataIndex: "majorName",
            align: "left"
        },  {
            title: "系统编码",
            dataIndex: "majorSysCode",
            align: "left"
        }, {
            title: "专业方向代码",
            dataIndex: "majorCode",
            align: "left"
        },  {
            title: "开始时间",
            dataIndex: "startDate",
            align: "left"
        }, {
            title: "结束时间",
            dataIndex: "endDate",
            align: "left"
        },
        {
            title: "操作",
            align: "left",
            render: (item) => {
                return (
                    <div>
                        <Popconfirm title="是否要删除？" onConfirm={() => handleDelete(item)}>
                            <Button style={{ padding: 2, color: "#C21217", fontWeight: "bold" }} type="link">删除</Button>
                        </Popconfirm>

                    </div>
                );
            }
        }
    ];
    return (
        <div className={styles.pageContainer}>
            <div className={styles.pageContainerHeader}>
                <SectionTitle title={title}/>
                <div style={{ width: "220px" }} onClick={() => addItem({})}>
                    <AddSomeButton title={title}/>
                </div>
            </div>
            <div className={styles.pageContainerTable}>
                <SectionTitle title={"已有任务运行"}/>
                <div className={styles.filterContainer}>
                    <Select
                        placeholder="培训类型"
                        className={styles.formWrapper}
                        value={trainingTypeId}
                        onChange={changeSelect}
                    >
                        {
                            trainingTypeList.map(item => {
                                return <Option key={item.trainTypeId}
                                               value={item.trainTypeId}>{item.trainingTypeName}</Option>;
                            })
                        }
                    </Select>
                    <Button
                        className={styles.marginRight}
                        type="primary"
                        onClick={() => {
                            fetch(trainingTypeId);
                        }}
                    >
                        查询
                    </Button>
                    <Button
                        onClick={() => {
                            fetch("");
                            setTrainingTypeId("");
                        }}
                    >
                        重置
                    </Button>
                </div>
                <div className={styles.tableContainer}>
                    <Table
                        bordered={false} // 是否有边框
                        rowKey='runTrainTaskId' // 唯一key值
                        loading={loading} // 加载动画
                        columns={columns} // 表头
                        dataSource={dataSource}
                        pagination={false}
                    />
                </div>
            </div>
            <Modal
                title={title}
                visible={visible}
                width="860px"
                onOk={() => {
                    form.validateFields().then(async values => {
                        values.startDate = moment(values.time[0]).format("YYYY-MM-DD");
                        values.endDate = moment(values.time[1]).format("YYYY-MM-DD");
                        delete values.time;
                        const result = await createRunTrainTaskInfo(values);
                        if (result.status === "success") {
                            setVisible(false);
                            fetch("");
                        } else {
                            message.error(result.message);
                        }
                    }).catch(info => {
                        // console.log(info);
                    });
                }}
                onCancel={() => {
                    form.resetFields();
                    setVisible(false);
                }}
            >

                <Form
                    {...layout}
                    layout="horizontal"
                    form={form}
                    name="basic"
                    initialValues={{}}
                >
                    <Form.Item
                        label="培训类型"
                        name="trainingTypeId"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <Select
                            placeholder="培训类型"
                            onChange={(e) => {
                                fetchMajorDirectionList(e);
                            }}
                        >
                            {
                                trainingTypeList.map(item => {
                                    return <Option key={item.trainTypeId}
                                                   value={item.trainTypeId}>{item.trainingTypeName}</Option>;
                                })
                            }
                        </Select>
                    </Form.Item>
                    <Form.Item
                        label="专业方向"
                        name="majorDirectionId"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <Select
                            placeholder="专业方向"
                        >
                            {
                                majorDirectionList.map(item => {
                                    return <Option key={item.majorDirectionId}
                                                   value={item.majorDirectionId}>{item.majorName}</Option>;
                                })
                            }
                        </Select>
                    </Form.Item>
                    <Form.Item
                        label="起止时间"
                        name="time"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <RangePicker style={{ width: "100%" }}/>
                    </Form.Item>

                    <Form.Item
                        label="招生人数"
                        name="recruitNum"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <Input placeholder="请输入招生人数 "/>
                    </Form.Item>
                    <Form.Item
                        label="实际培训人数 "
                        name="actualTrainingNum"
                        rules={[{ required: true, message: "必填" }]}
                    >
                        <Input placeholder="请输入实际培训人数 "/>
                    </Form.Item>
                </Form>
            </Modal>
        </div>
    );

};

export default TrainTask;
